package com.web.activiti.controller;

import com.web.activiti.pojo.ActiveUser;
import com.web.activiti.pojo.Employee;
import com.web.activiti.pojo.Leavebill;
import com.web.activiti.service.WorkFlowService;
import com.web.activiti.utils.Contants;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.task.Comment;
import org.activiti.engine.task.Task;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
public class WorkFlowController {
    @Autowired
    private WorkFlowService workFlowService;

    /**
     * 发布流程开始
     * @param processName
     * @param processFile
     * @return
     */
    @RequestMapping("/deployProcess")
    public String deployProcess(String processName, MultipartFile processFile){
        System.out.println("deployProcess...执行");
        try {
            workFlowService.delployProcess(processName, processFile.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
        return "redirect:/processDefinitionList";
    }

    @RequestMapping("/processDefinitionList")
    public ModelAndView processDefinitionList(){
        System.out.println("processDefinitionList...执行完毕，跳转。。。。。");
        //流程部署信息
        List<Deployment> deployList=workFlowService.findAllDeployments();
        //流程部署信息
        List<ProcessDefinition> pdList=workFlowService.findAllProcessDefinitions();
        ModelAndView mv = new ModelAndView();
        mv.addObject("pdList",pdList);
        mv.addObject("depList", deployList);
        mv.setViewName("workflow_list");
        return mv;
    }

    /*  发布流程结束  */

    /**
     * 请假流程
     * @param leavebill
     * @param session
     * @return
     */
    @RequestMapping("/saveStartLeave")
    public String saveStartLeave(Leavebill leavebill, HttpSession session){
        Employee employee = (Employee) session.getAttribute(Contants.GLOBAL_SESSION_ID);
        workFlowService.saveStartProcess(leavebill,employee );
        return "redirect:/myTaskList";
    }

    /**
     * 跳转代办事项页
     * @param session
     * @return
     */
    @RequestMapping("/myTaskList")
    public ModelAndView myTaskList(HttpSession session){
        ActiveUser employee = (ActiveUser) session.getAttribute(Contants.GLOBAL_SESSION_ID);
        List<Task> taskList = workFlowService.findTaskListByUserId(employee.getUsername());
        ModelAndView mv = new ModelAndView();
        mv.addObject("taskList", taskList);
        mv.setViewName("workflow_task");
        return mv;
    }

    @Test
    public void fun() {
        UUID uuid = UUID.randomUUID();
        System.out.println("uuid = " + uuid);
    }


    /**
     * 进入办理页面  并打印出批注
     * @param taskId
     * @return
     */
    @RequestMapping("/viewTaskForm")
    public ModelAndView viewTaskForm(String taskId){
        System.out.println("进入viewTaskForm。。。。");
        //根据流程数据的 数据查s询请假业务表的数据 business_key
        Leavebill leavebill=workFlowService.findBillByTask(taskId);
        //查询批注
        List<Comment> commentList = workFlowService.findCommentList(taskId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("bill", leavebill);
        mv.addObject("commentList", commentList);
        mv.addObject("taskId", taskId);
        mv.setViewName("approve_leave");
        return mv;
    }

//    /**
//     * 推进流程
//     * @param id
//     * @param taskId
//     * @param comment
//     * @param session
//     * @return
//     */
//    @RequestMapping("/submitTask")
//    public String submitTask(long id , String taskId,String comment,HttpSession session){
//        Employee employee = (Employee) session.getAttribute(Contants.GLOBAL_SESSION_ID);
//        workFlowService.saveSubmitTask(id,taskId,comment,employee.getName());
//        return "redirect:/myTaskList";
//    }


}
